<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2022/12/5
 * Time: 13:25
 */

namespace app\controller\sys;
use app\model\tb\TbUser AS model;
use app\model\app\AppUserBasic AS basic;
class User extends Base
{
    public function list(){
        $where = $this->where;
        $post = input('post.');
        !empty($post['username']) && $where[] = ['username','like','%'.$post['username'].'%'];
        !empty($post['mobile']) && $where[] = ['mobile','like','%'.$post['mobile'].'%'];
        !empty($post['email']) && $where[] = ['email','like','%'.$post['email'].'%'];
        !empty($post['user_type']) && $where[] = ['user_type','=',$post['user_type']];
        !empty($post['need_pay']) && $where[] = ['need_pay','=',$post['need_pay']];
        !empty($this->start_time) && $where[] = ['create_time','>=',$this->start_time];
        !empty($this->end_time) && $where[] = ['create_time','<=',$this->end_time];
        $list = model::where($where)
            ->with(['basic'])
            ->order('create_time DESC')
            ->paginate([
                'page' => $this->page,
                'list_rows' => $this->size
            ]);
        return $this->msg($list);
    }

    public function info(){
        $id = input('user_id');
        $user = model::where('user_id',$id)->with(['basic'])->find();
        return $this->msg($user);
    }

    public function save(){
        $post = input('post.');
        if (empty($post['user_id'])) {

            model::create($post);
        }else {
            if (is_array($post['user_id'])) {
                $user_id = $post['user_id'];
                unset($post['user_id']);
                model::where('user_id','IN',$user_id)->update($post);
            } else {
                $find = model::where('user_id', $post['user_id'])->find();
                if (empty($find->user_id))
                    return $this->fail('数据不存在');
                $find->save($post);
            }
        }
        return $this->msg();
    }

    public function del(){
        $user_id = input('user_id');
        $find = model::where('user_id',$user_id)->find();
        if (empty($find->user_id))
            return $this->fail('数据不存在');
        $find->delete();
        return $this->msg();
    }

    public function basic_save(){
        $post = input('post.');
        if (empty($post['id'])) {
            basic::create($post);
        }else {
            $find = basic::where('id', $post['id'])->find();
            if (empty($find->id))
                return $this->fail('数据不存在');
            $find->save($post);
        }
        return $this->msg();
    }
}